#include "savemanager.h"
#include "QStringList"
#include "QFile"
#include "errormanager.h"

SaveManager* SaveManager::instance;
QStringList bindings;
QStringList configs;


SaveManager::SaveManager()
{

}

bool SaveManager::BindingsOK()
{
    QStringList temporary = bindings;
    temporary.removeAll(",");
    return (temporary.removeDuplicates()== 0 && (!bindings.contains("CTRL,,") && !bindings.contains("ALT,,") && !bindings.contains("SHIFT,,") && !temporary.isEmpty()));
}

void SaveManager::ClearBindings(){
    bindings.clear();
}

void SaveManager::AddBindings(QString &s)
{
    if(bindings.size()==5)
        bindings.clear();

    bindings.append(s);

}

void SaveManager::AddConfigs(QString &s)
{
    if(configs.size()==1)
        configs.clear();
    configs.append(s);
}

void SaveManager::SaveConfigs(QString path)
{
    QFile f(path);
    f.open( QIODevice::WriteOnly );

    QString content;
    foreach(QString config, configs)
    {
        content.append(config.append(","));
    }

    content.truncate(content.length()-1);
    f.write(content.toUtf8());

    f.close();
}

void SaveManager::SaveBindings(QString path)
{
    QFile f(path);
    f.open( QIODevice::WriteOnly );
    QString content;

    foreach(QString binding, bindings)
    {
        content.append(binding.append(","));
    }

    content.truncate(content.length()-1);
    f.write(content.toUtf8());
    f.close();

}

void SaveManager::ClearConfigs(){
    configs.clear();
}

QStringList SaveManager::GetBindings()
{
    return bindings;
}

double SaveManager::GetVolume(){
    if(configs.empty())
        return 50.0;

    return configs.at(0).toDouble();

}

SaveManager *SaveManager::GetInstance()
{
    if(instance == NULL)
        instance = new SaveManager();


    return instance;
}
